<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Module_Manager{

    public $compressableExtensions = array(
        'php',
        'xml',
        'txt',
        'html',
        'htm',
        'tpl'
    );



    public function rebuildHookCache(){
        $CONFIG = JFX::registry('config');
        $DB = JFX::registry('db');

        $themeHooks = array();
        $moduleHooks = array();


        $installedModules = JFX_Module::getInstalledModules();
        $installedThemes = JFX_Theme::getInstalledThemes();

        foreach($installedModules as $k=>$v){
            if(!is_dir($CONFIG->modulesDir.'/'.$v['object']->keyname.'/hooks')) continue;
            $dh = opendir($CONFIG->modulesDir.'/'.$v['object']->keyname.'/hooks');
            while($file = readdir($dh)){
                if($file == '.' || $file == '..') continue;
                if(end(explode('.', $file))!=='php') continue;
                $hook = array(
                    'module' => $v['object']->keyname,
                    'hook' => $file
                );
                $moduleHooks[] = $hook;
            }
        }


        foreach($installedThemes as $k=>$v){
            if(!is_dir($CONFIG->themesDir.'/'.$v['object']->keyname.'/hooks')) continue;
            $dh = opendir($CONFIG->themesDir.'/'.$v['object']->keyname.'/hooks');
            while($file = readdir($dh)){
                if($file == '.' || $file == '..') continue;
                $hook = array(
                    'theme' => $v['object']->keyname,
                    'hook' => $file
                );
                $themeHooks[] = $hook;
            }
        }

        $DB->query('TRUNCATE '.$CONFIG->dbprefix.'hooks');

        foreach($moduleHooks as $k=>$v){
            $dbParams = array(
                'keyname' => $v['module'],
                'hook' => $v['hook'],
                'is_module' => 1,
                'sorting' => $k
            );
            $DB->insert($CONFIG->dbprefix.'hooks', $dbParams);
        }
        $k2 = $k;
        foreach($themeHooks as $k=>$v){
            $dbParams = array(
                'keyname' => $v['theme'],
                'hook' => $v['hook'],
                'is_theme' => 1,
                'sorting' => $k2+$k
            );
            $DB->insert($CONFIG->dbprefix.'hooks', $dbParams);
        }


    }




    public function import($filename, $destDir){
        $CONFIG = JFX::registry('config');
        if(!is_dir($destDir)) mkdir($destDir);
        $fc = file_get_contents($filename);
        $dir = unserialize($fc);
        $this->extractDirFromArray($dir, $destDir);
    }

    public function extractDirFromArray($array, $dirname){
        foreach($array as $k=>$v){
            if($v['t'] == 'd'){ // type != directory
                if(!is_dir($dirname.'/'.$v['fn'])) mkdir($dirname.'/'.$v['fn']); // filename
                $this->extractDirFromArray($v['fs'], $dirname.'/'.$v['fn']); // files, filename
            }else if($v['t']=='f'){ // type == file
                $fh = fopen($dirname.'/'.$v['fn'], 'w'); // filename
                if($v['c']) $fc = $this->decompress($v['fc']); // compressed, file content
                else $fc = $v['fc']; // file content
                fwrite($fh, $fc);
                fclose($fh);
            }
        }
    }

    public function compress($content){
        return $content;
        $BWT = JFX::registry('ThirdParty_BWT');
        $MTF = JFX::registry('ThirdParty_MTF');
        return $MTF->encode($BWT->transform($content));
    }

    public function decompress($content){
        return $content;
        $BWT = JFX::registry('ThirdParty_BWT');
        $MTF = JFX::registry('ThirdParty_MTF');
        return $BWT->inverse($MTF->decode($content));
    }

/*
    public function compress($content){
        $LZW = JFX::registry('ThirdParty_LZW');
        return $LZW->compress($content);
    }

    public function decompress($content){
        $LZW = JFX::registry('ThirdParty_LZW');
        return $LZW->decode($content);
    }
*/

    public function export($moduleKey, $filename){

        $isModule = JFX_Module::isValidModule($moduleKey);
        $isTheme = JFX_Theme::isValidTheme($moduleKey);
        
        if(!$isModule && !$isTheme) return false;

        $CONFIG = JFX::registry('config');

        // compresses all file content and loads a recursive array containing all files and directories
        if($isTheme){
            $dirname = $CONFIG->themesDir.'/'.$moduleKey;
        }else{
            $dirname = $CONFIG->modulesDir.'/'.$moduleKey;
        }
        $dir = $this->loadDirAsArray($dirname);
        $dir['m'] = $moduleKey;
        $dir['is_theme'] = (int) $isTheme;
echo '<pre>';
        var_dump($dir);
echo '</pre>';
        $fc = serialize($dir);
        $fh = fopen($filename, 'w');
        fwrite($fh, $fc);
        fclose($fh);
        return true;

    }

    public function loadDirAsArray($dirname){
        $files = array();
        $dh = opendir($dirname);
        while($file = readdir($dh)){
            if($file == '.' || $file == '..') continue;
            if(is_dir($dirname.'/'.$file)){
                $files[] = array(
                    't' => 'd', // type = directory
                    'fn' => $file, // filename
                    'fs' => $this->loadDirAsArray($dirname.'/'.$file) // files
                );
            }else{
                $pathinfo = pathinfo($dirname.'/'.$file);
                if(in_array($pathinfo['extension'], $this->compressableExtensions)){
                    $compressed = true;
                    $fc = $this->compress(file_get_contents($dirname.'/'.$file));
                }
                else{
                    $compressed = false;
                    $fc = file_get_contents($dirname.'/'.$file);
                }
                $files[] = array(
                    't' => 'f', // type = file
                    'fn' => $file, // filename
                    'c' => $compressed, // compressed
                    'fc' => $fc // file content
                );
            }
        }



        return $files;
    }



}